<template>
  <div class="app-container">
    <!-- 查询 -->
    <div class="filter-container">
      <el-row :gutter="0">
        <el-col :xs="24" :sm="22">
          <!-- <el-input v-model="query.username" class="filter-item" style="width: 200px;" placeholder="用户名" clearable />
          <el-input v-model="query.fullname" class="filter-item" style="width: 200px;" placeholder="姓名" clearable />
          <el-input v-model="query.module_url" class="filter-item" style="width: 200px;" placeholder="模块链接" clearable />
          <el-input v-model="query.module_name" class="filter-item" style="width: 200px;" placeholder="模块名称" clearable />
          <el-input v-model="query.request_ip" class="filter-item" style="width: 200px;" placeholder="请求IP" clearable />
          <el-button class="filter-item" type="primary" @click="search()">查询</el-button> -->
          <el-button class="filter-item" @click="refresh()">刷新</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="datas" :height="height" style="width: 100%" border>
      <el-table-column prop="id" label="ID" min-width="40" sortable="custom" fixed="left" />
      <el-table-column prop="username" label="用户名" min-width="60" />
      <el-table-column prop="fullname" label="姓名" min-width="60" />
      <el-table-column prop="module_url" label="模块链接" min-width="80" />
      <el-table-column prop="module_name" label="模块名称" min-width="80" />
      <el-table-column prop="request_method" label="请求方式 " min-width="60" />
      <el-table-column prop="request_ip" label="请求IP" min-width="60" />
      <el-table-column prop="request_region" label="请求地区" min-width="60" />
      <el-table-column prop="request_isp" label="请求ISP" min-width="60" />
      <el-table-column prop="create_time" label="请求时间" min-width="60" sortable="custom" />
      <el-table-column label="操作" min-width="60" align="right" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="success" @click="edit(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="count > 0" :total="count" :page.sync="query.page" :limit.sync="query.limit" @pagination="list" />
    <!-- 添加、修改 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialog" top="1vh" width="50%" :before-close="cancel">
      <el-form ref="ref" :rules="rules" :model="model" label-width="100px" class="dialog-body" :style="{height:height+30+'px'}">
        <el-form-item label="用户ID" prop="user_id">
          <el-col :span="10">
            <el-input v-model="model.user_id" />
          </el-col>
          <el-col class="line" :span="4" style="text-align:center">用户昵称</el-col>
          <el-col :span="10">
            <el-input v-model="model.fullname" />
          </el-col>
        </el-form-item>
        <el-form-item label="用户账号" prop="username">
          <el-input v-model="model.username" />
        </el-form-item>
        <el-form-item label="模块ID" prop="module_id">
          <el-col :span="10">
            <el-input v-model="model.module_id" />
          </el-col>
          <el-col class="line" :span="4" style="text-align:center">模块名称</el-col>
          <el-col :span="10">
            <el-input v-model="model.module_name" />
          </el-col>
        </el-form-item>
        <el-form-item label="模块链接" prop="menu_url">
          <el-input v-model="model.module_url" />
        </el-form-item>
        <el-form-item label="请求方式" prop="request_method">
          <el-col :span="10">
            <el-input v-model="model.request_method" />
          </el-col>
          <el-col class="line" :span="4" style="text-align:center">请求IP</el-col>
          <el-col :span="10">
            <el-input v-model="model.request_ip" />
          </el-col>
        </el-form-item>
        <el-form-item label="请求地区" prop="request_region">
          <el-col :span="10">
            <el-input v-model="model.request_region" />
          </el-col>
          <el-col class="line" :span="4" style="text-align:center">请求ISP</el-col>
          <el-col :span="10">
            <el-input v-model="model.request_isp" />
          </el-col>
        </el-form-item>
        <el-form-item label="请求时间" prop="create_time">
          <el-input v-model="model.create_time" />
        </el-form-item>
        <el-form-item label="请求参数" prop="request_param">
          <pre>{{ model.request_param }}</pre>
        </el-form-item>
        <el-form-item label="返回码" prop="response_code">
          <el-input v-model="model.response_code" />
        </el-form-item>
        <el-form-item label="返回描述" prop="response_msg">
          <el-input v-model="model.response_msg" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import screenHeight from '@/utils/screen-height'
import Pagination from '@/components/Pagination'
import { list, info } from '@/api/userlog'

export default {
  name: 'userlog',
  components: { Pagination },
  data() {
    return {
      height: 680,
      loading: false,
      datas: [],
      count: 0,
      query: {
        page: 1,
        limit: 13
      },
      dialog: false,
      dialogTitle: '',
      model: {
        parent_id: '',
        id: '',
        name: '',
        url: '',
        permission_code:'',
        sort: 200,
        remark: ''
      },
      rules: {
        name: [{ required: true, message: '请输入模块名称', trigger: 'blur' }],
        parent_id: [{ required: true, message: '请输入父模块Id', trigger: 'blur' }],
      },
      menuTree: [],
      userDialog: false,
      userDialogTitle: '',
      userLoad: false,
      userData: [],
      userCount: 0,
      userQuery: {}
    }
  },
  created() {
    this.height = screenHeight()
    this.list()
  },
  methods: {
    // 列表
    list() {
      this.loading = true
      list(this.query).then(res => {
        this.datas = res.data.list
        this.count = res.data.count
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 修改
    edit(row) {
      this.dialog = true
      this.dialogTitle = '详情：' + row.id
      // menuTree().then(res => {
      //   this.menuTree = res.data.list
      // })
      info({
        id: row.id
      }).then(res => {
        this.reset(res.data)
      })
    },
    // 取消
    cancel() {
      this.dialog = false
      this.reset()
    },
    // 重置
    reset(row) {
      if (row) {
        this.model = row
      } else {
        this.model = this.$options.data().model
      }
      if (this.$refs['ref'] !== undefined) {
        this.$refs['ref'].resetFields()
      }
    },
    // 查询
    search() {
      this.query.page = 1
      this.list()
    },
    // 刷新
    refresh() {
      this.query = this.$options.data().query
      this.list()
    },
    // 确认
    submit() {
      this.reset()
      this.dialog = false
    },
  }
}
</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
